package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.mapper.SysLoginLogMapper;
import com.mask.im.business.model.entity.SysLoginLogEntity;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.SysLoginLogService;
import com.mask.im.business.common.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户登录日志管理
 *
 * @author jian
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLogEntity> implements SysLoginLogService {

    @Override
    public PageResult<SysLoginLogEntity> loginRecordList(ConditionVO conditionVO) {

        Page<SysLoginLogEntity> objectPage = new Page<>(PageUtil.getLimitPage(), PageUtil.getSize());

        LambdaQueryWrapper<SysLoginLogEntity> lambdaQueryWrapper = new LambdaQueryWrapper<SysLoginLogEntity>()
                .eq(Objects.nonNull(conditionVO.getId()), SysLoginLogEntity::getUserId, conditionVO.getId())
                .like(!StringUtils.isEmpty(conditionVO.getKeywords()), SysLoginLogEntity::getTitle, conditionVO.getKeywords())
                .orderByDesc(SysLoginLogEntity::getCreateTime);

        Page<SysLoginLogEntity> loginRecordsPage = baseMapper.selectPage(objectPage, lambdaQueryWrapper);
        return new PageResult<SysLoginLogEntity>(Math.toIntExact(loginRecordsPage.getTotal()), loginRecordsPage.getRecords(), 20000);
    }

    @Override
    public RestResponse<String> deleteLoginRecord(List<Integer> loginRecordIds) {
        boolean byIds = this.removeByIds(loginRecordIds);
        if (byIds) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}

